package znbkBkfx;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.znbk.znbklibrary.base.BaseLongActivity;
import com.example.znbk.znbklibrary.util.LancooUtils;
import com.google.gson.Gson;
import com.lancoo.znbkxx.R;
import com.lancoo.znbkxx.adapter.SampleAdapter;
import com.lancoo.znbkxx.beans.UserInfoBean;
import com.example.znbk.znbklibrary.dialog.ExitDialog;
import com.example.znbk.znbklibrary.widget.ScaleButton;
import com.lancoo.znbkxx.net.URLConstant;
import com.lancoo.znbkxx.uis.PhoneMainActivity;

import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;
import znbkBkfx.BkfxResultAnalysis.BkfxPaperResultActivity;
import znbkBkfx.GetBkfxJson.BkfxPaperEntity;
import znbkBkfx.GetBkfxJson.GetRequest_BkfxInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import znbkBkfx.GetBkfxPaperResultJson.BkfxPaperResultEntity;
import znbkBkfx.GetBkfxPaperResultJson.GetRequest_BkfxPaperResultInterface;
import znbkBkfx.PostSaveItemQues.ItemAnswerModel;
import znbkBkfx.PostSubmitPaper.PostSubmitPaper_interface;

public class BkfxMainInforActivity extends BaseLongActivity {

    @Override
    protected int setLayout() {
        return R.layout.activity_bkfx_main_infor;
    }

    public void doBusiness() {
        /*处理业务操作*/
        clickBack();//处理返回按钮点击事件
        StartReadAnim(500);//处理跳转准备动画
        showTopBarTitle();
        showPaperInfor();
    }

    private void clickBack(){
        mSBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showExitDialog();
            }
        });
    }

    private void showTopBarTitle(){
        mSbtnTopbarTitle.setText(bkfxPaperEntity.getPaperName());
    }

    private void showPaperInfor(){
        int PaperSize = bkfxPaperEntity.getQuesList().size();
        int FinishTypeCount = bkfxPaperEntity.getFinishTypeCount();
        Double FullSocre = bkfxPaperEntity.getFullScore();
        int State = bkfxPaperEntity.getState();
        if(State==0){
            mTvPaperInfor.setText(Html.fromHtml(getResources().
                    getString(R.string.paper_infor_initialization,PaperSize,(new Double(FullSocre)).intValue())));
        }else{
            mTvPaperInfor.setText(Html.fromHtml(getResources().
                    getString(R.string.paper_infor_learning,PaperSize,FinishTypeCount,(new Double(FullSocre)).intValue())));
        }

    }

    private void StartReadAnim(long afterSomeTimes){
        final Handler mHandler = new Handler();
        Runnable mRunnable = new Runnable() {
            @Override
            public void run() {
                showReady3Anim();
                mHandler.removeCallbacks(this);
            }
        };
        mHandler.postDelayed(mRunnable,afterSomeTimes);
    }
    private void showReady3Anim(){
        /*
         * 组合动画
         * */
        ObjectAnimator scalexShow =  ObjectAnimator.ofFloat(mIvReady3,View.SCALE_X,0f,1.2f,1.0f);
        ObjectAnimator scaleYShow =  ObjectAnimator.ofFloat(mIvReady3,View.SCALE_Y,0f,1.2f,1.0f);
        AnimatorSet setShow = new AnimatorSet();
        setShow.playTogether(scalexShow,scaleYShow);
        setShow.setInterpolator(new DecelerateInterpolator());
        setShow.setDuration(100);

        setShow.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                mIvReady3.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                mIvReady3.setVisibility(View.GONE);
                showReady2Anim();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        setShow.start();
    }
    private void showReady2Anim(){
        /*
         * 组合动画
         * */
        ObjectAnimator scalexShow =  ObjectAnimator.ofFloat(mIvReady2,View.SCALE_X,0f,1.2f,1.0f);
        ObjectAnimator scaleYShow =  ObjectAnimator.ofFloat(mIvReady2,View.SCALE_Y,0f,1.2f,1.0f);
        AnimatorSet setShow = new AnimatorSet();
        setShow.playTogether(scalexShow,scaleYShow);
        setShow.setInterpolator(new DecelerateInterpolator());
        setShow.setDuration(100);

        setShow.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                mIvReady2.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                mIvReady2.setVisibility(View.GONE);
                showReady1Anim();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        setShow.start();
    }
    private void showReady1Anim(){
        /*
         * 组合动画
         * */
        ObjectAnimator scalexShow =  ObjectAnimator.ofFloat(mIvReady1,View.SCALE_X,0f,1.2f,1.0f);
        ObjectAnimator scaleYShow =  ObjectAnimator.ofFloat(mIvReady1,View.SCALE_Y,0f,1.2f,1.0f);
        AnimatorSet setShow = new AnimatorSet();
        setShow.playTogether(scalexShow,scaleYShow);
        setShow.setInterpolator(new DecelerateInterpolator());
        setShow.setDuration(100);

        setShow.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                mIvReady1.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                mIvReady1.setVisibility(View.GONE);
                showReadyGoAnim();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        setShow.start();
    }
    private void showReadyGoAnim(){
        /*
         * 组合动画
         * */
        ObjectAnimator scalexShow =  ObjectAnimator.ofFloat(mIvReadyGo,View.SCALE_X,0f,1.2f,1.0f);
        ObjectAnimator scaleYShow =  ObjectAnimator.ofFloat(mIvReadyGo,View.SCALE_Y,0f,1.2f,1.0f);
        AnimatorSet setShow = new AnimatorSet();
        setShow.playTogether(scalexShow,scaleYShow);
        setShow.setInterpolator(new DecelerateInterpolator());
        setShow.setDuration(100);

        setShow.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                mIvReadyGo.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                mIvReadyGo.setVisibility(View.INVISIBLE);

                finish();
                Intent intent = new Intent();
                intent.setClass(BkfxMainInforActivity.this, BkfxActivity.class);
                startActivity(intent);

            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        setShow.start();
    }

    @Override
    public void onBackPressed() {
        showExitDialog();
    }

    public void showExitDialog(){
        //暂停计时
        //初始化显示弹窗
        ExitDialog.Builder builder = new ExitDialog.Builder(this);
        builder.setTitle("温馨提示")
                .setMessage("是否要退出本次训练···")
                .setExitButton("退出训练", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                        Intent intent = new Intent();
                        intent.setClass(BkfxMainInforActivity.this, PhoneMainActivity.class);
                        startActivity(intent);
                    }
                })
                .setContinueButton("继续训练", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //继续计时
                    }
                }).create().show();
    }


    private ScaleButton mSBtnBack;//返回按钮
    private ScaleButton mSbtnTopbarTitle;//头部试卷详情标题
    private LinearLayout mLlPaperInforLoading;
    private LinearLayout mLlPaperInforShow;
    private ImageView mIvReady3,mIvReady2,mIvReady1,mIvReadyGo;
    private TextView mTvPaperInfor;
    protected void initView() {
        requestBkfxPaperInfor();
        mSBtnBack = findViewById(R.id.sbtn_back);
        mSbtnTopbarTitle = findViewById(R.id.sbtn_topbarTitle);
        mLlPaperInforLoading = findViewById(R.id.ll_paperInforLoading);
        mLlPaperInforLoading.setVisibility(View.VISIBLE);
        mLlPaperInforShow = findViewById(R.id.ll_paperInforShow);
        mLlPaperInforShow.setVisibility(View.GONE);
        mIvReady3 = findViewById(R.id.iv_ready3);
        mIvReady2 = findViewById(R.id.iv_ready2);
        mIvReady1 = findViewById(R.id.iv_ready1);
        mIvReadyGo = findViewById(R.id.iv_readyGo);
        mTvPaperInfor = findViewById(R.id.tv_paperInfor);
    }

    /*********************************************************************************************/
    public static BkfxPaperEntity bkfxPaperEntity = null;
    public void requestBkfxPaperInfor() {
        String BASE_URL = URLConstant.BASE_URL;
        //步骤4:创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl("http://fy.iciba.com/") // 设置 网络请求 Url

                .baseUrl(BASE_URL) // 设置 网络请求 Url
                .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖)
                .build();

        // 步骤5:创建 网络请求接口 的实例
        final GetRequest_BkfxInterface request = retrofit.create(GetRequest_BkfxInterface.class);

        //对 发送请求 进行封装
//        Call<BkfxPaperEntity> call = request.getCall();
        Log.e("==显示BBBBB==","============UserInfoBean.SchoolID==============="+ UserInfoBean.SchoolID);
        Log.e("==显示BBBBB==","============UserInfoBean.SubjectID================"+ UserInfoBean.SubjectID);
        Log.e("==显示BBBBB==","============UserInfoBean.GlobalGrade==============="+ UserInfoBean.GlobalGrade);
        Log.e("==显示BBBBB==","============UserInfoBean.CourseClassID================"+ UserInfoBean.CourseClassID);
        Log.e("==显示BBBBB==","============PhoneMainActivity.getPaperId()================"+ PhoneMainActivity.getPaperId());
        Log.e("==显示BBBBB==","============UserInfoBean.StuId================"+ UserInfoBean.StuId);
        Log.e("==显示BBBBB==","============UserInfoBean.Key================"+ UserInfoBean.Key);
        Call<BkfxPaperEntity> call = request.getPaperDetail(UserInfoBean.Key,UserInfoBean.SchoolID,
                UserInfoBean.SubjectID,UserInfoBean.GlobalGrade,UserInfoBean.CourseClassID,
                PhoneMainActivity.getPaperId(),UserInfoBean.StuId,true,true,1);
        Log.e("===onResponse==","=====call========="+call);

        //步骤6:发送网络请求(异步)
        call.enqueue(new Callback<BkfxPaperEntity>() {
            //请求成功时候的回调
            @Override
            public void onResponse(Call<BkfxPaperEntity> call, Response<BkfxPaperEntity> response) {
                //请求处理,输出结果
                Log.e("===call==","=====bkfxPaper========="+response);
                bkfxPaperEntity = response.body();
                Log.e("===onResponse==","=====fromAAA========="+bkfxPaperEntity.getDuration());
                if(bkfxPaperEntity!=null) {

                    int state = bkfxPaperEntity.getState();
                    if(state==2||state==3){
                        mSbtnTopbarTitle.setText(bkfxPaperEntity.getPaperName());
                        requestBkfxPaperResult();
                    }else{
                        mLlPaperInforLoading.setVisibility(View.GONE);
                        mLlPaperInforShow.setVisibility(View.VISIBLE);

                        doBusiness();
                    }
                }else{
                    LancooUtils.showShort(BkfxMainInforActivity.this,"试卷解析出错");
                }

            }

            //请求失败时候的回调
            @Override
            public void onFailure(Call<BkfxPaperEntity> call, Throwable throwable) {
                System.out.println("连接失败");
                LancooUtils.showShort(BkfxMainInforActivity.this,"网络出错,试卷解析出错");
//                UserInfoBean.bIsGetData = false;
                Log.e("==显示AAAA==","======连接失败======bIsGetData====================="+ UserInfoBean.bIsGetData );
            }
        });
    }

    public static BkfxPaperResultEntity bkfxPaperResultEntity = null;
    public void requestBkfxPaperResult() {
        String BASE_URL = URLConstant.BASE_URL;
        //步骤4:创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl("http://fy.iciba.com/") // 设置 网络请求 Url

                .baseUrl(BASE_URL) // 设置 网络请求 Url
                .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖)
                .build();

        // 步骤5:创建 网络请求接口 的实例
        final GetRequest_BkfxPaperResultInterface request = retrofit.create(GetRequest_BkfxPaperResultInterface.class);

        //对 发送请求 进行封装
        Log.e("==显示BBBBB==","============UserInfoBean.SchoolID==============="+ UserInfoBean.SchoolID);
        Log.e("==显示BBBBB==","============UserInfoBean.SubjectID================"+ UserInfoBean.SubjectID);
        Log.e("==显示BBBBB==","============UserInfoBean.GlobalGrade==============="+ UserInfoBean.GlobalGrade);
        Log.e("==显示BBBBB==","============UserInfoBean.CourseClassID================"+ UserInfoBean.CourseClassID);
        Log.e("==显示BBBBB==","============PhoneMainActivity.getPaperId()================"+ PhoneMainActivity.getPaperId());
        Log.e("==显示BBBBB==","============UserInfoBean.StuId================"+ UserInfoBean.StuId);
        Call<BkfxPaperResultEntity> call = request.getPaperAnalysis(UserInfoBean.Key,UserInfoBean.CourseClassID,UserInfoBean.GlobalGrade,
                PhoneMainActivity.getPaperId(),true,true,UserInfoBean.SchoolID,1,
                UserInfoBean.StuId,UserInfoBean.SubjectID);

        Log.e("===onResponse==","=====call========="+call);

        //步骤6:发送网络请求(异步)
        call.enqueue(new Callback<BkfxPaperResultEntity>() {
            //请求成功时候的回调
            @Override
            public void onResponse(Call<BkfxPaperResultEntity> call, Response<BkfxPaperResultEntity> response) {
                //请求处理,输出结果
                bkfxPaperResultEntity = response.body();
                Log.e("===onResponse==","=====response========="+response);
                if(bkfxPaperResultEntity!=null){
                    finish();
                    Intent intent = new Intent();
                    intent.setClass(BkfxMainInforActivity.this, BkfxPaperResultActivity.class);
                    startActivity(intent);
                }else{
                    LancooUtils.showShort(BkfxMainInforActivity.this,"作答结果解析出错");
                }
            }

            //请求失败时候的回调
            @Override
            public void onFailure(Call<BkfxPaperResultEntity> call, Throwable throwable) {
                System.out.println("连接失败");
                LancooUtils.showShort(BkfxMainInforActivity.this,"连接失败，显示加载失败占位图");
            }
        });
    }
    /**********************************************************************************************/

}
